package com.mobileares.midp.widgets.client.tab;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.*;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-7-14
 * Time: 9:35:45
 * To change this template use File | Settings | File Templates.
 */
public class FrameTabItem  extends SimplePanel {
    private SimplePanel text;
    private FrameTabPanel tabBar;
    private Anchor refresh;
    private Anchor close;
    private SimplePanel left;
    private FlowPanel right;
    private Widget showWidget;

    private boolean hasClose = true;
    private boolean hasRefresh = true;

    private Object userObject;

    public FishTableItemEvent getEvent() {
        return event;
    }

    private FishTableItemEvent event;


    public FrameTabItem(FrameTabPanel tabBar, Widget show) {
        setStyleName("fish-tab-item");

        left = new SimplePanel();
        left.setStyleName("fish-tab-item-l");
        right = new FlowPanel();
        right.setStyleName("fish-tab-item-r");
        text = new SimplePanel();
        text.setStyleName("fish-tab-item-text");
        refresh = new TabAnchor("刷新");
        refresh.setStyleName("fresh");
        refresh.setVisible(false);
        close = new TabAnchor("关闭");
        close.setStyleName("close");
        close.setVisible(false);
        right.add(text);
        right.add(refresh);
        right.add(close);

        left.add(right);
        this.tabBar = tabBar;
        this.showWidget = show;
        sinkEvents(Event.ONCLICK);
        sinkEvents(Event.MOUSEEVENTS);
        sinkEvents(Event.ONDBLCLICK);
        initEvent();
        super.setWidget(left);
    }

    public FrameTabItem(FrameTabPanel tabBar, Widget show, FishTableItemEvent event) {
        this(tabBar, show);
        setEvent(event);
    }


    public Widget getShowWidget() {
        return showWidget;
    }

    public void setShowWidget(Widget widget) {
        this.showWidget = widget;
    }

    public void setEvent(FishTableItemEvent event) {
        this.event = event;
    }


    public void setText(String w) {
        text.setWidget(new HTML(w));
    }

    public String getText() {
        return ((HTML) (text.getWidget())).getText();
    }

    public Object getUserObject() {
        return userObject;
    }

    public void setUserObject(Object userObject) {
        this.userObject = userObject;
    }

    private void initEvent() {
        addHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                tabBar.setSelectItem(FrameTabItem.this);
            }
        }, ClickEvent.getType());
        refresh.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (FrameTabItem.this.event != null)
                    FrameTabItem.this.event.onRefresh();
            }
        });
        close.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (FrameTabItem.this.event != null)
                    FrameTabItem.this.event.onClose();
                tabBar.closeTabItem(FrameTabItem.this);
            }
        });
        addHandler(new DoubleClickHandler() {

            public void onDoubleClick(DoubleClickEvent event) {
                if (hasClose) {
                    if (FrameTabItem.this.event != null)
                        FrameTabItem.this.event.onClose();
                    tabBar.closeTabItem(FrameTabItem.this);
                }
            }
        }, DoubleClickEvent.getType());
    }

    public void addSelectedStyle() {
        addStyleDependentName("selected");
        left.addStyleDependentName("selected");
        right.addStyleDependentName("selected");
        if (hasRefresh)
            refresh.setVisible(true);
        if (hasClose)
            close.setVisible(true);
    }

    public void removeSelectedStyle() {
        removeStyleDependentName("selected");
        left.removeStyleDependentName("selected");
        right.removeStyleDependentName("selected");
        refresh.setVisible(false);
        close.setVisible(false);
    }

    public void setClose(boolean hasClose) {
        this.hasClose = hasClose;
    }

    public void setRefresh(boolean hasRefresh) {
        this.hasRefresh = hasRefresh;
    }


    public void onBrowserEvent(Event event) {
        switch (DOM.eventGetType(event)) {
            case Event.ONMOUSEOVER:
                addStyleDependentName("enable");
                right.addStyleDependentName("enable");
                left.addStyleDependentName("enable");
                break;
            case Event.ONMOUSEOUT:
                removeStyleDependentName("enable");
                right.removeStyleDependentName("enable");
                left.removeStyleDependentName("enable");
                break;
//            case Event.ONDBLCLICK:
//                System.out.println("saad");
        }
        super.onBrowserEvent(event);
    }

    @Override
    protected void onLoad() {
        tabBar.onLoad();
    }

    public interface FishTableItemEvent {
        void onSelect();

        void onRefresh();

        void onClose();
    }

    private class TabAnchor extends Anchor {

        TabAnchor(String text) {
            super(text);
        }

        @Override
        public void onBrowserEvent(Event event) {
            event.stopPropagation();
            super.onBrowserEvent(event);
        }
    }
}

